package com.ruoyi.develop.payRecord.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.finance.financeAp.service.IFinanceApService;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.mapper.LvBuyMapper;
import com.ruoyi.develop.orderIronRecordBuy.domain.OrderIronRecordBuy;
import com.ruoyi.develop.orderIronRecordBuy.mapper.OrderIronRecordBuyMapper;
import com.ruoyi.develop.payApply.domain.PayApply;
import com.ruoyi.develop.payApply.mapper.PayApplyMapper;
import com.ruoyi.develop.payApplyBuyDetail.domain.PayApplyBuyDetail;
import com.ruoyi.develop.payApplyBuyDetail.mapper.PayApplyBuyDetailMapper;
import com.ruoyi.develop.payRecordBuyDetail.domain.PayRecordBuyDetail;
import com.ruoyi.develop.payRecordBuyDetail.mapper.PayRecordBuyDetailMapper;
import com.ruoyi.develop.process.processPayApply.service.IProcessPayApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.payRecord.mapper.PayRecordMapper;
import com.ruoyi.develop.payRecord.domain.PayRecord;
import com.ruoyi.develop.payRecord.service.IPayRecordService;

/**
 * 付款记录Service业务层处理
 *
 * @author lmm
 * @date 2024-10-27
 */
@Service
public class PayRecordServiceImpl implements IPayRecordService {
    @Resource
    private PayRecordMapper payRecordMapper;
    @Resource
    private PayRecordBuyDetailMapper payRecordBuyDetailMapper;
    @Resource
    private PayApplyBuyDetailMapper payApplyBuyDetailMapper;
    @Resource
    private OrderIronRecordBuyMapper orderIronRecordBuyMapper;
    @Resource
    private LvBuyMapper lvBuyMapper;
    @Resource
    private PayApplyMapper payApplyMapper;
    @Resource
    private IProcessPayApplyService processPayApplyService;
    @Autowired
    private IFinanceApService financeApService;

    /**
     * 查询付款记录
     *
     * @param id 付款记录主键
     * @return 付款记录
     */
    @Override
    public PayRecord selectById(String id) {
        return payRecordMapper.selectById(id);
    }

    /**
     * 查询付款记录列表
     *
     * @param entity 付款记录
     * @return 付款记录
     */
    @Override
    public List<PayRecord> selectList(PayRecord entity) {
        return payRecordMapper.selectList(entity);
    }

    /**
     * 新增付款记录
     *
     * @param entity 付款记录
     * @return 结果
     */
    @Override
    public int insert(PayRecord entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if(StringUtils.isBlank(entity.getNos())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(entity.getPayTime()==null?new Date():entity.getPayTime());
            String codeMaxString = payRecordMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            entity.setNos(codeMaxString);
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return payRecordMapper.insert(entity);
    }

    /**
     * 修改付款记录
     *
     * @param entity 付款记录
     * @return 结果
     */
    @Override
    public int update(PayRecord entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return payRecordMapper.update(entity);
    }

    /**
     * 批量删除付款记录
     *
     * @param ids 需要删除的付款记录主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return payRecordMapper.deleteByIds(ids);
    }

    @Override
    public int cancelByIds(String[] ids) {
        for(String id : ids) {
            PayRecord payRecord = payRecordMapper.selectById(id);
            payRecord.setStatus(BusinessConstants.PAY_RECORD_STATUS_CANCEL);
            payRecordMapper.update(payRecord);
            PayApply payApply = payApplyMapper.selectById(payRecord.getApplyId());
            payApply.setPaidMoney(payApply.getPaidMoney().subtract(payRecord.getMoney()));
            payApply.setRemainMoney(payApply.getRemainMoney().add(payRecord.getMoney()));
            processPayApplyService.insertProcess(payApply.getId(), "", "取消打款："+payRecord.getMoney()+"元");

            if(payApply.getRemainMoney().compareTo(BigDecimal.ZERO) == 0){
                payApply.setPayStatus(BusinessConstants.PAY_APPLY_PAY_STATUS_PAID);
            }else if(payApply.getPaidMoney().compareTo(BigDecimal.ZERO) == 0){
                payApply.setPayStatus(BusinessConstants.PAY_APPLY_PAY_STATUS_UN);
            }else{
                payApply.setPayStatus(BusinessConstants.PAY_APPLY_PAY_STATUS_PART);
            }
            payApplyMapper.update(payApply);
            LvBuy lvBuy = lvBuyMapper.selectById(payApply.getBuyId());
            lvBuy.setFukuan(lvBuy.getFukuan().subtract(payRecord.getMoney()));
            lvBuy.setShengyu(lvBuy.getShengyu().add(payRecord.getMoney()));
            lvBuyMapper.update(lvBuy);
            PayRecordBuyDetail payRecordBuyDetail = new PayRecordBuyDetail();
            payRecordBuyDetail.setPayRecordId(id);
            List<PayRecordBuyDetail> payRecordBuyDetailList = payRecordBuyDetailMapper.selectList(payRecordBuyDetail);
            if (payRecordBuyDetailList != null && payRecordBuyDetailList.size() > 0) {
                for (PayRecordBuyDetail payRecordBuyDetail1 : payRecordBuyDetailList) {
                    if(StringUtils.isNotBlank(payRecordBuyDetail1.getPayApplyBuyDetailId())){
                        PayApplyBuyDetail payApplyBuyDetail = payApplyBuyDetailMapper.selectById(payRecordBuyDetail1.getPayApplyBuyDetailId());
                        payApplyBuyDetail.setPayRemain(payApplyBuyDetail.getPayRemain().add(payRecordBuyDetail1.getMoney()));
                        payApplyBuyDetail.setPayMoney(payApplyBuyDetail.getPayMoney().subtract(payRecordBuyDetail1.getMoney()));
                        if(payApplyBuyDetail.getPayRemain().compareTo(BigDecimal.ZERO) == 0){
                            payApplyBuyDetail.setPayStatus(BusinessConstants.STATUS_YES);
                        }else{
                            payApplyBuyDetail.setPayStatus(BusinessConstants.STATUS_NO);
                        }
                        payApplyBuyDetailMapper.update(payApplyBuyDetail);
                    }

                    if (StringUtils.isNotBlank(payRecordBuyDetail1.getOrderIronRecordBuyId())) {
                        OrderIronRecordBuy orderIronRecordBuy = orderIronRecordBuyMapper.selectById(payRecordBuyDetail1.getOrderIronRecordBuyId());
                        orderIronRecordBuy.setPayRemain(orderIronRecordBuy.getPayRemain().add(payRecordBuyDetail1.getMoney()));
                        orderIronRecordBuy.setPayMoney(orderIronRecordBuy.getPayMoney().subtract(payRecordBuyDetail1.getMoney()));
                        if (orderIronRecordBuy.getPayRemain().compareTo(BigDecimal.ZERO) == 0) {
                            orderIronRecordBuy.setPayStatus(BusinessConstants.STATUS_YES);
                        }else{
                            orderIronRecordBuy.setPayStatus(BusinessConstants.STATUS_NO);
                        }
                        orderIronRecordBuyMapper.update(orderIronRecordBuy);
                    }

                }
            }

            // 付款之后 插入 应付账款
            financeApService.addByPayRecordCancel(payRecord);

        }

        return 1;
    }



    /**
     * 删除付款记录信息
     *
     * @param id 付款记录主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return payRecordMapper.deleteById(id);
    }
}
